package com.aizhixin.lab.project.course.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.course.classes.domain.QueryLabReportDomain;
import com.aizhixin.lab.project.course.domain.AddTeacherDomain;
import com.aizhixin.lab.project.course.domain.saveBftDomain;
import com.aizhixin.lab.project.course.service.TeacherProjectService;
import com.aizhixin.lab.project.course.vo.*;
import com.aizhixin.lab.project.template.data.NewProjectData;
import com.aizhixin.lab.project.template.domain.ModuleTaskDomain;
import com.aizhixin.lab.project.template.domain.ProjectDomain;
import com.aizhixin.lab.project.template.domain.QueryProjectDomain;
import com.aizhixin.lab.project.template.entity.Project;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/api/v1/teacher/project")
@RestController
@Api(value = "教师端项目模块", tags = "授课")
public class TeacherProjectController {
    @Autowired
    private TeacherProjectService projectService;
    @Autowired
    private AccountService accountService;

    @RequestMapping(value = "/findProjectInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查看项目详情", httpMethod = "GET", notes = "查看项目详情<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProjectDomain.class),})
    public ResponseEntity<?> findProjectInfo(@RequestHeader("Authorization") String token, @RequestParam(name = "id") String id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            NewProjectData projectDomain = projectService.findProjectDomain(id);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, projectDomain);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "保存", httpMethod = "POST", notes = "保存/编辑<br><br><b>lwq</b>")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token, @RequestBody ProjectDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            String save = projectService.save(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,save);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findMyPrepareLessons", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查看我的授课信息", httpMethod = "GET", notes = "查看我的授课信息<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = TeacherProjectVo.class),})
    public ResponseEntity<?> findMyPrepareLessons(@RequestHeader("Authorization") String token,
                                                  @ApiParam(value = "当值为0查询备课,值为1查询授课") @RequestParam Integer isPublish,
                                                  @ApiParam(value = "课程名称") @RequestParam String name,
                                                  @ApiParam(value = "pageNumber 起始页") @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
                                                  @ApiParam(value = "pageSize 每页的限制数目") @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }

            result = projectService.findMyProject(account.getId(), isPublish,pageNumber,pageSize,name);


        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findBftProject", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查看该课程下的备选课程", httpMethod = "GET", notes = "查看该课程下的备选课程<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = TeacherProjectVo.class),})
    public ResponseEntity<?> findBftProject(@RequestHeader("Authorization") String token, @ApiParam(value = "项目ID") @RequestParam String projectId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            List<TeacherProjectVo> teacherProjectVoList = projectService.findOtherProject(projectId, "备选项目");
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, teacherProjectVoList);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findTestProject", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查看该项目下的练习项目", httpMethod = "GET", notes = "查看该项目下的练习项目<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = TeacherProjectVo.class),})
    public ResponseEntity<?> findTestProject(@RequestHeader("Authorization") String token, @ApiParam(value = "项目ID") @RequestParam String projectId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            List<TeacherProjectVo> teacherProjectVoList = projectService.findOtherProject(projectId, "练习项目");
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, teacherProjectVoList);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findAllTeacherList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查看所询老师(不包括项目中已添加的老师)", httpMethod = "GET", notes = "查看所有老师<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = TeacherInfoVo.class),})
    public ResponseEntity<?> findAllTeacherList(@RequestHeader("Authorization") String token, @ApiParam(value = "项目ID") @RequestParam String projectId,
                                                @RequestParam String name) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            List<TeacherInfoVo> teacherInfoVoList = projectService.findAllOtherTeacher(projectId,name);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, teacherInfoVoList);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findProjectTeacherList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查看该项目下的老师", httpMethod = "GET", notes = "查看该项目下的老师(第一个老师为主讲老师)<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = TeacherInfoVo.class),})
    public ResponseEntity<?> findProjectTeacherList(@RequestHeader("Authorization") String token, @ApiParam(value = "项目ID") @RequestParam String projectId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            List<TeacherInfoVo> teacherInfoVoList = projectService.findProjectTeacherList(projectId);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, teacherInfoVoList);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/addProjectTeacher", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "添加其他讲师", httpMethod = "POST", notes = "添加其他讲师<br><br><b>lwq</b>")
    public ResponseEntity<?> addProjectTeacher(@RequestHeader("Authorization") String token, @RequestBody AddTeacherDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            result = projectService.addProjectTeacher(domain);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/publish", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "我要备课", httpMethod = "GET", notes = "备课<br><br><b>lwq</b>")
    public ResponseEntity<?> publish(@RequestHeader("Authorization") String token, @ApiParam(value = "项目ID") @RequestParam String projectId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            String publish = projectService.publish(projectId, account);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, publish);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/introduceRecently", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "引入授课", httpMethod = "GET", notes = "引入授课<br><br><b>lwq</b>")
    public ResponseEntity<?> introduceRecently(@RequestHeader("Authorization") String token, @ApiParam(value = "项目ID") @RequestParam String projectId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            String publish = projectService.introduceRecently(projectId);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, publish);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/delete", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除项目", httpMethod = "GET", notes = "删除项目<br><br><b>lwq</b>")
    public ResponseEntity<?> delete(@RequestHeader("Authorization") String token, @ApiParam(value = "项目ID") @RequestParam String projectId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
            result = projectService.delete(projectId,account.getId());

        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/addBftProject", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "添加备选项目或练习项目", httpMethod = "POST", notes = "添加备选项目或练习项目<br><br><b>lwq</b>")
    public ResponseEntity<?> addBftProject(@RequestHeader("Authorization") String token,
                                           @RequestBody saveBftDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            String publish = projectService.addBftProject(domain, account);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, publish);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/stuFindMyProject", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "学生查看自己参与的项目信息", httpMethod = "POST", notes = "学生查看自己参与的项目信息<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = StuProjectVo.class),})
    public ResponseEntity<?> stuFindMyProject(@RequestHeader("Authorization") String token,
                                              @RequestBody QueryProjectDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        result = projectService.findStuProject(domain, account.getId());

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findTaskProgress", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "项目组总体数据-任务进度", httpMethod = "GET", notes = "项目组总体数据-任务进度")
    public ResponseEntity<?> findTaskProgress(@RequestHeader("Authorization") String token,@ApiParam(value = "项目ID") @RequestParam String projectId
            ,@ApiParam(value = "小组ID") @RequestParam String groupId
    ) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
            ProjectTaskProgressVo publish = projectService.findTaskProgress(projectId,groupId);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,publish);

        return  new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findMemberContribute", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "项目组总体数据-成员贡献度", httpMethod = "GET", notes = "项目组总体数据-成员贡献度")
    public ResponseEntity<?> findMemberContribute(@RequestHeader("Authorization") String token,@ApiParam(value = "项目ID") @RequestParam String projectId
            ,@ApiParam(value = "小组ID") @RequestParam String groupId
    ) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            List<ProjectMemberContributeVo> publish = projectService.findMemberContribute(projectId,groupId);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,publish);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return  new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findReleaseTask", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "项目组总体数据-发布任务时间点折线图", httpMethod = "GET", notes = "项目组总体数据-发布任务时间点折线图")
    public ResponseEntity<?> findReleaseTask(@RequestHeader("Authorization") String token,@ApiParam(value = "项目ID") @RequestParam String projectId
            ,@ApiParam(value = "小组ID") @RequestParam String groupId
    ) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            Map publish = projectService.findReleaseTask(projectId,groupId);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,publish);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return  new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findStuGiveLike", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "项目组总体数据-查询点赞和提醒折线图", httpMethod = "GET", notes = "项目组总体数据-查询点赞和提醒折线图")
    public ResponseEntity<?> findStuGiveLike(@RequestHeader("Authorization") String token,@ApiParam(value = "项目ID") @RequestParam String projectId
            ,@ApiParam(value = "小组ID") @RequestParam String groupId
    ) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            Map publish = projectService.findStuGiveLike(projectId,groupId);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,publish);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return  new ResponseEntity(result, HttpStatus.OK);
    }


    @RequestMapping(value = "/findProjectBurnoutFgure", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "项目组总体数据-查询项目燃尽图", httpMethod = "GET", notes = "项目组总体数据-查询项目燃尽图")
    public ResponseEntity<?> findProjectBurnoutFgure(@RequestHeader("Authorization") String token,@ApiParam(value = "项目ID") @RequestParam String projectId
            ,@ApiParam(value = "小组ID") @RequestParam String groupId
    ) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }

            Map publish = projectService.findProjectBurnoutFgure(projectId,groupId);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,publish);

        return  new ResponseEntity(result, HttpStatus.OK);
    }



}
